package test;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-05
 * Time:19:46
 */
public class LoginPage extends Utils {
    public static String url = "http://81.70.248.108:58081/login.html";

    public LoginPage() {
        super(url);
    }
    /**
     * 登录页面可以正常打开检查页面元素
     */
    public void checkPageRight() throws IOException {
        //检查登录框
        webDriver.findElement(By.cssSelector("#username"));
        webDriver.findElement(By.cssSelector("#password"));
        webDriver.findElement(By.cssSelector("#submit"));
        webDriver.findElement(By.cssSelector("#register"));

        //屏幕截图
        //System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
        //ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 正常登录
     */
    public void LoginSuc(String userName,String password){
        //先清空输入框
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();

        //输入正确的用户名和密码：zhangsan,123
        webDriver.findElement(By.cssSelector("#username")).sendKeys(userName);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();

        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = webDriver.switchTo().alert();
        //切换到弹窗
        alert.dismiss();
    }

    /**
     * 异常登录
     */
    public void LoginFail(){
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();

        //1.用户密码都为空
        webDriver.findElement(By.cssSelector("#submit")).click();
        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert1 = webDriver.switchTo().alert();
        //切换到弹窗
        alert1.dismiss();
        //2.用户名为空
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#submit")).click();
        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert2 = webDriver.switchTo().alert();
        //切换到弹窗
        alert2.dismiss();

        //3.密码为空
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.findElement(By.cssSelector("#submit")).click();
        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert3 = webDriver.switchTo().alert();
        //切换到弹窗
        alert3.dismiss();

        //4.用户或密码错误
        webDriver.findElement(By.cssSelector("#username")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();

        //输入错误的用户名和正确的密码：zhangsan1,123
        webDriver.findElement(By.cssSelector("#username")).sendKeys("zhangsan1");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123");
        webDriver.findElement(By.cssSelector("#submit")).click();

        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert4 = webDriver.switchTo().alert();
        //切换到弹窗
        alert4.dismiss();
    }

}
